﻿//
//
//using System;
//using System.IO;
//using System.Text.RegularExpressions;
//
//using DanielVaughan.ComponentModel;
//using DanielVaughan.Data;
//using DanielVaughan.IO;
//
//namespace DanielVaughan.Services.Implementation
//{
//	public class FileService : IFileService
//	{
		//static readonly ILog log = LogManager.GetLogger(typeof(FileService));
//
//		public FileOperationResult SaveAs(string fileName, FileOperationHandler operationHandler,
//			FileErrorAction fileErrorAction)
//		{
//			ArgumentValidator.AssertNotNull(operationHandler, "operationHandler");
//
//			/* TODO: create save file dialog. */
//			var messageService = Dependency.Resolve<IMessageService>();
//			var question = new TextResponseQuestion(StringResources.Services_FileService_SaveFileDialogMessage);
//			TextResponse response = messageService.AskQuestionWithTextResponse(question);
//
//			if (response == null)
//			{
//				return FileOperationResult.Failed;
//			}
//
//			if (response.Result == OkCancelQuestionResult.Cancel)
//			{
//				return FileOperationResult.Cancelled;
//			}
//
//			if (response.Response == null)
//			{
//				/* A dedicated safe file dialog will prevent this. */
//				messageService.ShowWarning(string.Format("The file name is invalid."));
//				return FileOperationResult.Cancelled;
//			}
//
//			string sanitized = SanitizeFileName(response.Response);
//			if (string.IsNullOrEmpty(sanitized))
//			{
//				/* A dedicated safe file dialog will prevent this. */
//				messageService.ShowWarning(string.Format("The file name is invalid."));
//				return FileOperationResult.Cancelled;
//			}
//
//			return Save(sanitized, operationHandler, fileErrorAction);
//		}
//
//		static string SanitizeFileName(string name)
//		{
//			string invalidCharacters = Regex.Escape(new string(Path.GetInvalidPathChars()));
//			string replacements = string.Format(@"[{0}]+", invalidCharacters);
//			return Regex.Replace(name, replacements, "_");
//		}
//
//
//		public FileOperationResult Save(string fileName, FileOperationHandler operationHandler,
//			FileErrorAction fileErrorAction)
//		{
//			ArgumentValidator.AssertNotNullOrEmpty(fileName, "fileName");
//			ArgumentValidator.AssertNotNull(operationHandler, "operationHandler");
//
//			FileErrorAction actualFileErrorAction = fileErrorAction;
//
//			string userMessage = null;
//			bool ioExceptionOccured = false;
//			try
//			{
//				operationHandler(fileName);
//				return FileOperationResult.Successful;
//			}
//			catch (IOException ex)
//			{
//				log.Info("Unable to save file: " + fileName, ex);
//				userMessage = "A problem occured saving the file."; /* TODO: Make localizable resource. */
//				ioExceptionOccured = true;
//			}
//			catch (Exception ex) /* TODO: catch common IO errors and report to user. */
//			{
				//log.Info("Unable to save file: " + fileName, ex);
//				var userMessageException = ex as IUserMessageProvider;
//				if (userMessageException != null && userMessageException.UserMessagePresent)
//				{
//					userMessage = userMessageException.UserMessage;
//				}
//			}
//
//			if (!ioExceptionOccured)
//			{
//				if (fileErrorAction == FileErrorAction.UseAlternative)
//				{	/* If this wasn't an IOException then we do not allow 
//					 * the user to select an alternative. */
//					actualFileErrorAction = FileErrorAction.InformOnly;
//				}
//			}
//
//			var result = FileOperationResult.Failed;
//
//			var messageService = Dependency.Resolve<IMessageService>();
//
//			switch (actualFileErrorAction)
//			{
//				case FileErrorAction.UseAlternative:
//					messageService.ShowError(userMessage, StringResources.Services_FileService_UnableToSaveFile);
//					do
//					{
//						result = SaveAs(fileName, operationHandler, FileErrorAction.InformOnly);
//					} while (result != FileOperationResult.Successful && result != FileOperationResult.Cancelled);
//					break;
//				case FileErrorAction.InformOnly:
//					messageService.ShowError(userMessage, StringResources.Services_FileService_UnableToSaveFile);
//					break;
//			}
//			return result;
//		}
//
//		public FileOperationResult Open(FileOperationHandler operationHandler,
//			FileErrorAction fileErrorAction, string dialogFilter)
//		{
//			ArgumentValidator.AssertNotNull(operationHandler, "operationHandler");
//			return Open(null, operationHandler, fileErrorAction, dialogFilter);
//		}
//
//		public FileOperationResult Open(string filePath, FileOperationHandler operationHandler,
//			FileErrorAction fileErrorAction, string dialogFilter)
//		{
//			ArgumentValidator.AssertNotNull(operationHandler, "operationHandler");
//
//			string fileNameToUse = filePath;
//			if (!File.Exists(filePath))
//			{
//				var shell = Dependency.Resolve<IMainWindow>() as Window;
//				FileOperationResult result = FileOperationResult.Successful;
//				var dispatcher = Dependency.Resolve<Dispatcher>();
//				dispatcher.InvokeIfRequired(delegate
//				{
//					var dialog = new OpenFileDialog();
//					/* TODO: Customize the save dialog. */
//					dialog.Title = "Choose file name";
//					dialog.Filter = dialogFilter;
//					if (!dialog.ShowDialog(shell).Value)
//					{
//						result = FileOperationResult.Cancelled;
//						return;
//					}
//					fileNameToUse = dialog.FileName;
//				});
//				if (result != FileOperationResult.Successful)
//				{
//					return result;
//				}
//			}
//
//			string userMessage = null;
//			try
//			{
//				operationHandler(fileNameToUse);
//				return FileOperationResult.Successful;
//			}
//			catch (MementoIncompatibilityException ex)
//			{
//				userMessage = string.Format(StringResources.Services_FileService_FileCreatedInNewerVersion, fileNameToUse);
				//log.Info("Unable to open file: " + filePath, ex);
//			}
//			catch (Exception ex) /* TODO: catch common IO errors and report to user. */
//			{
				//log.Info("Unable to open file: " + filePath, ex);
//			}
//
//			if (userMessage == null)
//			{
//				userMessage = string.Format(StringResources.Services_FileService_UnableToOpenFile, fileNameToUse);
//			}
//
//			var messageService = Dependency.Resolve<IMessageService>();
//
//			switch (fileErrorAction)
//			{
//				case FileErrorAction.UseAlternative:
//					messageService.ShowWarning(userMessage);
//					var result = Open(null, operationHandler, FileErrorAction.UseAlternative, dialogFilter);
//					return result;
//				case FileErrorAction.InformOnly:
//					messageService.ShowWarning(userMessage);
//					break;
//			}
//			return FileOperationResult.Failed;
//		}
//
//	}
//}
